package com.datamation.swadeshi.view;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.LoadingDetAdapter;
import com.datamation.swadeshi.adapter.ProductAdapter;
import com.datamation.swadeshi.adapter.SetTitleAdapter;
import com.datamation.swadeshi.control.ListExpandHelper;
import com.datamation.swadeshi.control.ReferenceNum;
import com.datamation.swadeshi.control.Toast;
import com.datamation.swadeshi.control.data.DebtorDS;
import com.datamation.swadeshi.control.data.FTranDetDS;
import com.datamation.swadeshi.control.data.FTranHedDS;
import com.datamation.swadeshi.control.data.ItemPriDS;
import com.datamation.swadeshi.control.data.ItemsDS;
import com.datamation.swadeshi.control.data.LocationsDS;
import com.datamation.swadeshi.control.data.OrdHedDS;
import com.datamation.swadeshi.control.data.RouteDS;
import com.datamation.swadeshi.control.data.SalRepDS;
import com.datamation.swadeshi.control.data.SharedPreferencesClass;
import com.datamation.swadeshi.model.FTranDet;
import com.datamation.swadeshi.model.FTranHed;
import com.datamation.swadeshi.model.Items;
import com.datamation.swadeshi.model.Locations;
import com.datamation.swadeshi.model.OrdHed;

import android.app.ActionBar;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

public class FragmentInventoryControlUnLoadingHeader extends Fragment {
	View view;
	private ActionBar actionBar;
	EditText Date1,locfrom,locto,manuref,remark;
	Toast  toast;
	ReferenceNum referenceNum;
	ListView lv_invent_load;
	ArrayList<FTranDet> loadlist;
	String FromCode,ToCode;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.inventory_control_unloadingheader, container, false);
		toast = new Toast();
		
       
		SetTitleAdapter objTitle =new SetTitleAdapter();
		objTitle.createCutomActionBarTitle("UnLoading Header", getActivity());
		referenceNum=new  ReferenceNum(getActivity());
		
		Button previousview = (Button)view.findViewById(R.id.btn_viewprevi);
		final EditText RefNo = (EditText)view.findViewById(R.id.sa_header_order_no);
		Date1 = (EditText)view.findViewById(R.id.sa_header_date);
		Button searchfrom = (Button)view.findViewById(R.id.btn_locfrom_search);
		Button searchto = (Button)view.findViewById(R.id.btn_locto_search);
		locfrom = (EditText)view.findViewById(R.id.etlocfrom_item);
		locto  = (EditText)view.findViewById(R.id.etlocto_item);
		Button selectitem = (Button)view.findViewById(R.id.btnselectitem);
		manuref = (EditText)view.findViewById(R.id.sa_header_manual_no);
		remark = (EditText)view.findViewById(R.id.sa_header_remark);
		loadtemp();

		
		
		RefNo.setText(referenceNum.getCurrentRefNo(getResources().getString(R.string.UnLodNumVal)));
		
		
		searchfrom.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				switch (v.getId()) 
				{
				case R.id.btn_locfrom_search:
					loctDetailsDialogbox("LT3","");
					break;
				
				default:
					break;
				}
				
			}
		});
		
		searchto.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) 
				{
				case R.id.btn_locto_search:
					loctDetailsDialogbox("LT1","");
					break;
				
				default:
					break;
				}
			}
		});
	
		previousview.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				// TODO Auto-generated method stub
				Viewbox();
			}
		});
		
		selectitem.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				// TODO Auto-generated method stub
				if(locfrom.getText().toString().equals("") || locto.getText().toString().equals(""))
				{
					toast.createToastMessage("Please select Locations", getActivity());	
				}
				else 
				{
					MainActivity activity = (MainActivity) getActivity();
					//RouteDS routeDS = new  RouteDS(getActivity());
					SalRepDS repDS=new SalRepDS(activity);
					FTranHed hed =new FTranHed();
					
					if(activity.selectFtranHed !=null)
						hed =activity.selectFtranHed;//set already enter values objects
					
					hed.setFTRANHED_REFNO(RefNo.getText().toString());
					hed.setFTRANHED_TXNDATE(Date1.getText().toString());
					hed.setFTRANHED_MANUREF(manuref.getText().toString());
					hed.setFTRANHED_COSTCODE("000");
					hed.setFTRANHED_REMARKS(remark.getText().toString());
					hed.setFTRANHED_TXNTYPE("41");
					hed.setFTRANHED_TOTALAMT("0");
					hed.setFTRANHED_DELPERSN("");
					hed.setFTRANHED_DELADD1("");
					hed.setFTRANHED_DELADD2("");
					hed.setFTRANHED_DELADD3("");
					hed.setFTRANHED_VEHICALNO("");
					hed.setFTRANHED_PRTCOPY("0");
					hed.setFTRANHED_GLPOST("");
					hed.setFTRANHED_GLBATCH("");
					hed.setFTRANHED_ADDUSER(repDS.getCurrentRepCode());
					hed.setFTRANHED_DEALCODE("");
					hed.setFTRANHED_LONGITUDE("0.000");
					hed.setFTRANHED_LATITUDE("0.000");
					hed.setFTRANHED_LOCFROM(FromCode);
					hed.setFTRANHED_LOCTO(ToCode);
					hed.setFTRANHED_IS_SYNCED("0");
					hed.setFTRANHED_ACTIVE_STATE("1");
					hed.setFTRANHED_ADDDATE(Date1.getText().toString());
					hed.setFTRANHED_ADDMACH("f0:6a:24:c2:6c:8e");

					
					activity.selectFtranHed = hed;//new updated object (new data + already enter data)

					ArrayList<FTranHed> ordHedList=new ArrayList<FTranHed>();
					FTranHedDS ftranhed =new  FTranHedDS(getActivity());
					//head
					ordHedList.add(activity.selectFtranHed);
					if(ftranhed.createOrUpdateFTranHed(ordHedList)>0)
					{
						getActivity().getActionBar().setSelectedNavigationItem(1);
						toast.createToastMessage("Sucessfully save", getActivity());
					}else 
					{
						getActivity().getActionBar().setSelectedNavigationItem(1);
						toast.createToastMessage("Sucessfully update", getActivity());
					}
					   
				}
				
			}
		});
		
		
		currentDate();

		
		return view;
	}
	
	public void loctDetailsDialogbox(final String loctcode,String searchword)
	{		
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.inventory_control_searchloc);
		
		final SearchView search = (SearchView) dialog.findViewById(R.id.et_search);
		final ListView locList=(ListView) dialog.findViewById(R.id.lv_product_list); 
		
		
		dialog.setCancelable(true);
		
		LocationsDS dsg=new LocationsDS(getActivity());
		final ArrayList<String> list= dsg.getlocDetails(loctcode,"");
	    ArrayAdapter<String> categoriesAdapter6 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
		locList.clearTextFilter();
		locList.setAdapter(categoriesAdapter6);
		locList.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) 
			{
				
				if(loctcode == "LT3")
				{
					locfrom.setText(parent.getItemAtPosition(position).toString().split("-:-")[0]);
					FromCode = parent.getItemAtPosition(position).toString().split("-:-")[1];
					
					
				}
				else if (loctcode == "LT1") 
				{
					locto.setText(parent.getItemAtPosition(position).toString().split("-:-")[0]);
					ToCode = parent.getItemAtPosition(position).toString().split("-:-")[1];
				}
				else 
				{
					
				}
				
			
				dialog.dismiss();
			}
		});
		
			
		search.setOnQueryTextListener(new OnQueryTextListener() 
		{
			
			@Override
			public boolean onQueryTextSubmit(String query) 
			{
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) 
			{
				LocationsDS locds =new LocationsDS(getActivity());
				locList.clearTextFilter();
				final ArrayList<String> list= locds.getlocDetails(loctcode,newText);
				ArrayAdapter<String> categoriesAdapter6 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
				locList.setAdapter(categoriesAdapter6);
				return false;
			}
		});				
		dialog.show();
	}
	public void Viewbox() 
	{
		final Dialog dialog = new Dialog(getActivity());
		
		dialog.setTitle("Previous Day UnLoading");
		dialog.setContentView(R.layout.inventory_control_unloadingprevioussummery);
		dialog.setCancelable(true);
		
		DecimalFormat dFormat = new DecimalFormat("000000");
		TextView VrefNo = (TextView)dialog.findViewById(R.id.view_refno);
		TextView VmanurefNo = (TextView)dialog.findViewById(R.id.view_manuno);
		TextView Vdate = (TextView)dialog.findViewById(R.id.view_date);
		TextView Vremark = (TextView)dialog.findViewById(R.id.view_remarks);
		TextView Vlocfrom = (TextView)dialog.findViewById(R.id.view_locfrom);
		TextView VlocTo = (TextView)dialog.findViewById(R.id.view_locto);
		final ListView lv_invent_load = (ListView) dialog.findViewById(R.id.lv_previsummery);
		dialog.show();
		
		Double DF =Double.parseDouble( referenceNum.getCurrentRefNo(getResources().getString(R.string.UnLodNumVal)).split("/")[1].toString())-1;		
		
		
		FTranHedDS hedDS =new FTranHedDS(getActivity());
		ArrayList<FTranHed> getFtranhed = hedDS.getAllActiveFtranHed(referenceNum.getCurrentRefNo(getResources().getString(R.string.UnLodNumVal)).split("/")[0]+"/"+dFormat.format(DF));
		try {

			ArrayList<FTranDet> loadlist= new FTranDetDS(getActivity()).getAllloadDetails(referenceNum.getCurrentRefNo(getResources().getString(R.string.UnLodNumVal)).split("/")[0]+"/"+dFormat.format(DF));
			
			Log.v("Len", loadlist.size()+"");
			
			lv_invent_load.setAdapter(new LoadingDetAdapter(getActivity(),loadlist));
			
		}catch (NullPointerException e)
		{
			Log.v("UnLoading Error", e.toString());
		}
		if(!getFtranhed.isEmpty())
		{
				
				for (FTranHed ftrn : getFtranhed)
				{
				
					VrefNo.setText(ftrn.getFTRANHED_REFNO());
					Vdate.setText(ftrn.getFTRANHED_TXNDATE());
					Vlocfrom.setText(ftrn.getFTRANHED_LOCFROM());
					VlocTo.setText(ftrn.getFTRANHED_LOCTO());
					VmanurefNo.setText(ftrn.getFTRANHED_MANUREF());
					Vremark.setText(ftrn.getFTRANHED_REMARKS());
				}
				
			}
	}
	
	private void currentDate() {
		// TODO Auto-generated method stub
		//current date and time 
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				Date date = new Date();
				Date1.setText(dateFormat.format(date));
				//deliveryDate.setText(dateFormat.format(date));
	}
	public void loadtemp() 
	{
		FTranHedDS hedDS =new FTranHedDS(getActivity());
		ArrayList<FTranHed> getFtranhed = hedDS.getAllActiveFtranHed(referenceNum.getCurrentRefNo(getResources().getString(R.string.UnLodNumVal)));
		
	if(!getFtranhed.isEmpty())
	{
		LocationsDS LOCF =new LocationsDS(getActivity());
			for (FTranHed ftrn : getFtranhed)
			{
								

				locfrom.setText(LOCF.getItemNameByCode(ftrn.getFTRANHED_LOCFROM()));
				locto.setText(LOCF.getItemNameByCode(ftrn.getFTRANHED_LOCTO()));

				FromCode = ftrn.getFTRANHED_LOCFROM();
				ToCode = ftrn.getFTRANHED_LOCTO();
				manuref.setText(ftrn.getFTRANHED_MANUREF());
				remark.setText(ftrn.getFTRANHED_REMARKS());

			}
			
		}
	
		
	}


}
